package com.fjd.service.impl;

import com.fjd.entity.Discount;
import com.fjd.mapper.DiscountDetailMapper;
import com.fjd.mapper.DiscountMapper;
import com.fjd.mapper.ProductCategoryMapper;
import com.fjd.service.DiscountService;
import com.fjd.vo.Category4VO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

@Service
public class DiscountServiceImpl implements DiscountService {
    @Autowired
    private DiscountMapper discountMapper;
    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    @Autowired
    private DiscountDetailMapper discountDetailMapper;

    @Override
    public List<Discount> list() {
        List<Discount> discounts = discountMapper.list();
        return discounts;
    }

    @Override
    public void add(Discount discount) {
        String id = UUID.randomUUID().toString().replace("-","");
        discount.setId(id);
        discountMapper.add(discount);

        List<Category4VO> category4VOS = productCategoryMapper.getInfoByParentId(discount.getMerchandiseId());
        for (Category4VO category4VO : category4VOS) {
            discountDetailMapper.add(category4VO.getProductId(), BigDecimal.valueOf(category4VO.getPrice()*discount.getRate()), discount.getId());
        }
    }

    @Override
    public void update(Discount discount) {
        discountMapper.update(discount);
    }

    @Transactional
    public void updateBatch(List<Discount> discountList) {
        for (Discount discount : discountList) {
            discountMapper.update(discount);
        }
    }

}
